﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace EnvControlLib
{
    /// <summary>
    /// 自动关闭窗口
    /// </summary>
    public partial class AutoCloseNoticeWindow : Window
    {
        #region 变量

        private int counter = 0;
        private int closeTime = 2; // 默认时间（秒）
        private string notice = "";
        private DispatcherTimer closeTimer;

        private static string imgUrl = "../Res/Msg_Notice.png"; // 默认显示图标 -- 静态变量

        #endregion

        #region 构造函数

        private AutoCloseNoticeWindow()
        {
            InitializeComponent();

            counter = closeTime;
            // 注册系统事件
            okButton.Click += new RoutedEventHandler(okButton_Click);
            this.Loaded += new RoutedEventHandler(AutoCloseNoticeWindow_Loaded);
        }

        private AutoCloseNoticeWindow(string text)
            : this()
        {
            this.noticeBlock.Text = text;
            this.okButton.Content = string.Format("关闭 ({0})", counter);
        }

        private AutoCloseNoticeWindow(string text, int autoCoseTime)
            : this(text)
        {
            counter = autoCoseTime;
        }

        #endregion

        #region 方法

        public static void Show(string text)
        {
            AutoCloseNoticeWindow acnw = new AutoCloseNoticeWindow(text);
            acnw.img.Source = new BitmapImage(new Uri(imgUrl, UriKind.RelativeOrAbsolute));
            acnw.ShowDialog();

            imgUrl = "../Res/Msg_Notice.png"; // 还原图标
        }

        public static void Show(string text, int closeTime)
        {
            AutoCloseNoticeWindow acnw = new AutoCloseNoticeWindow(text, closeTime);
            acnw.img.Source = new BitmapImage(new Uri(imgUrl, UriKind.RelativeOrAbsolute));
            acnw.ShowDialog();

            imgUrl = "../Res/Msg_Notice.png"; // 还原图标
        }

        /// <summary>
        /// 可显示 Error 图标（重载）
        /// </summary>
        /// <param name="text"></param>
        /// <param name="isError">True: 显示红色 Error；False: 显示黄色 Error</param>
        public static void Show(string text, bool isError)
        {
            if (isError)
                imgUrl = "../Res/Msg_Error.png";

            Show(text);
        }

        /// <summary>
        /// 可显示 Error 图标，带自定义时间（重载）
        /// </summary>
        /// <param name="text"></param>
        /// <param name="closeTime"></param>
        /// <param name="isError">True: 显示红色 Error；False: 显示黄色 Error</param>
        public static void Show(string text, int closeTime, bool isError)
        {
            if (isError)
                imgUrl = "../Res/Msg_Error.png";

            Show(text, closeTime);
        }

        #endregion

        #region 注册到外部的事件

        private void AutoCloseNoticeWindow_Loaded(object sender, RoutedEventArgs e)
        {
            closeTimer = new DispatcherTimer();
            closeTimer.Interval = new TimeSpan(0, 0, 0, 1);
            closeTimer.Tick += new EventHandler(closeTimer_Tick);
            closeTimer.Start();
        }

        private void closeTimer_Tick(object sender, EventArgs e)
        {
            if (counter-- == 1)
            {
                closeTimer.Stop();
                this.DialogResult = true;
                this.okButton.Content = string.Format("关闭 ({0})", counter); // 显示最后的一秒

                return;
            }

            this.okButton.Content = string.Format("关闭 ({0})", counter);
        }

        #endregion

        #region 与界面绑定的属性

        public int CloseTime
        {
            get { return closeTime; }
            set { closeTime = value; }
        }

        public string Notice
        {
            get { return notice; }
            set { notice = value; }
        }

        #endregion

        #region 系统事件

        /// <summary>
        /// 关闭事件窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            if (closeTimer != null && closeTimer.IsEnabled)
                closeTimer.Stop();

            this.DialogResult = true;
        }

        #endregion
    }
}
